package com.ry.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ry.novel.mapper.CategoryMapper;
import com.ry.novel.model.dto.CategoryInfo;
import com.ry.novel.model.entity.Category;
import com.ry.novel.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ry
 * @since 2022-02-18 08:21
 **/
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 获取所有的书籍分类
     *
     * @return
     */
    @Override
    public List<CategoryInfo> getAllCategory() {
        List<Category> categories = categoryMapper.selectList(new QueryWrapper<>());

        List<CategoryInfo> infos = categories
                .stream()
                .map(entity -> {
            CategoryInfo categoryInfo = new CategoryInfo();
            categoryInfo.setId(entity.getId());
            categoryInfo.setName(entity.getName());
            categoryInfo.setSort(entity.getSort());
            categoryInfo.setCreateTime(entity.getCreateTime());
            categoryInfo.setUpdateTime(entity.getUpdateTime());


            return categoryInfo;
        }).collect(Collectors.toList());

        return infos;
    }
}
